class ListNode{
    public int val;
    public ListNode prev;
    public ListNode next;

    public ListNode(int val) {
        this.val = val;
    }
}
public class MyLinkedList {
    public ListNode head;
    public ListNode last;
    public void display(){
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val + "");
            cur = cur.next;
        }
        System.out.println();
    }

    public int size(){
        ListNode cur = head;
        int count = 0;
        while (cur != null){
            count++;
            cur = cur.next;

        }
        return count;
    }
    public boolean contains(int val){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == val){
                return true;
            }else {
                cur = cur.next;
            }

        }
        return false;
    }

    public void addFirst(int data){
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            last = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }

    }

    public void addLast(int data){
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node ;
        }
    }

    public void remove(int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                if (cur == head){
                    head = head.next;
                    if (head != null){
                        head.prev = null;
                    }else {
                        last = null;
                    }

                }else {
                    cur.prev.next = cur.next;
                    if (cur.next != null){
                        cur.next.prev = cur.prev;

                    }else {
                        last = last.prev;
                    }

                }
            }else {
                cur = cur.next;
            }
        }
    }

    public void remove1(int key){
        ListNode cur = head;
        while (cur != null){
            if (cur.val == key){
                if (cur == head){
                    head = head.next;
                    if (head != null){
                        head.prev = null;
                    }else {
                        last = null;
                    }

                }else {
                    cur.prev.next = cur.next;

                    if (cur.next != null){
                        cur.next.prev = cur.prev;
                    }else {
                        last = last.prev;
                    }
                }
            }else {
                cur = cur.next;
            }
        }

    }

    public ListNode Search(int index){
        ListNode cur = head;
        while (index != 0){
              cur = cur.next;
              index--;
        }
        return cur;
    }
    public void addIndex(int index){
        ListNode node = new ListNode(index);
        if (index < 0 || index > size()){
            return ;
        }
        if (index == 0){
            addFirst(index);
            return ;
        }
        if (index == size()){
            addLast(index);
            return ;
        }
        ListNode cur = Search(index);

                    node.next = cur.prev.next;
                    cur.prev.next = node;
                    node.prev = cur.prev;
                    cur.prev = node;

    }

    public void clear(){


        while (head != null){
            ListNode cur = head.next;
            head.next = null;
            head.prev = null;

            head = cur;

        }
        last = null;

    }

    public static void main(String[] args) {
        Integer a = 129;
        Integer b = 129;
        System.out.println(a == b);//true
    }
}



